#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
typedef int BinaryType;

typedef struct BinaryTreeNode
{
	BinaryType a;
	struct BinaryTreeNode* left;
	struct BinaryTreeNode* right;
}BTNode;
typedef BTNode* QDatatype;

typedef struct QueueNode
{
	struct QueueNode* next;
	QDatatype data;
}QNode;

typedef struct Queue
{
	QNode* head;
	QNode* tail;
	int size;
}Queue;
void QueueInit(Queue* pq)
{
	assert(pq);
	pq->head = pq->tail = NULL;
	pq->size = 0;
}
void QueueDestroy(Queue* pq)
{
	assert(pq);
	QNode* cur = pq->head;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}

	pq->head = pq->tail = NULL;
	pq->size = 0;
}
void QueuePush(Queue* pq, QDatatype x)
{
	QNode* a = NULL;
	a = (QNode*)malloc(sizeof(QNode));
	a->data = x;
	a->next = NULL;
	if (pq->tail == NULL || pq->head == NULL)
	{
		pq->head = pq->tail = a;
		pq->size++;
	}
	else
	{
		pq->tail->next = a;
		pq->tail = a;
		pq->size++;
	}
}
void QueuePop(Queue* pq)
{
	QNode* a = pq->head;
	pq->head = pq->head->next;
	free(a);
	a = NULL;
	pq->size--;
}
QDatatype QueueFront(Queue* pq)
{
	assert(pq);
	return pq->head->data;
}
QDatatype QueueBack(Queue* pq)
{
	assert(pq->tail);
	return pq->tail->data;
}
bool QueueEmpty(Queue* pq)
{
	if (pq->head == pq->tail && pq->head == NULL)
	{
		return true;
	}
	else
	{
		return false;
	}
}
BTNode* BuyNode(int x)
{
	BTNode* a = NULL;
	a = (BTNode*)malloc(sizeof(BTNode));
	if (a == NULL)
	{
		perror("malloc error");
	}
	else
	{
		a->a = x;
		a->left = NULL;
		a->right = NULL;
		return a;
	}
}
BTNode* CreatBinaryTree()
{
	BTNode* node1 = BuyNode(1);
	BTNode* node2 = BuyNode(2);
	BTNode* node3 = BuyNode(3);
	BTNode* node4 = BuyNode(4);
	BTNode* node5 = BuyNode(5);
	BTNode* node6 = BuyNode(6);
	BTNode* node7 = BuyNode(6);
	node1->left = node2;
	node1->right = node3;
	node2->left = node4;
	node2->right = node5;

	return node1;
}
void PreOrder(BTNode* a)
{
	if (a == NULL)
	{
		printf("NULL ");
		return;
	}
	printf("%d ", a->a);
	PreOrder(a->left);
	PreOrder(a->right);
}
void InOrder(BTNode* a)
{
	if (a == NULL)
	{
		printf("NULL ");
		return;
	}
	InOrder(a->left);
	printf("%d ", a->a);
	InOrder(a->right);
}
void PostOrder(BTNode* a)
{
	if (a == NULL)
	{
		printf("NULL ");
		return;
	}
	PostOrder(a->left);
	PostOrder(a->right);
	printf("%d ", a->a);
}
int TreeSize(BTNode* a)
{
	return a == NULL ? 0 : (TreeSize(a->left) + TreeSize(a->right) + 1);
}
int TreeHight(BTNode* a)
{
	if (a == NULL)
	{
		return 0;
	}
	int b = TreeHight(a->left);
	int c = TreeHight(a->right);
	return b > c ? b + 1 : c + 1;
}
int TreekLevel(BTNode* a, int k)
{
	if (k == 1&&a!=NULL)
	{
		return 1;
	}
	if ( a == NULL)
	{
		return 0;
	}
	k--;
	return TreekLevel(a->left,k) + TreekLevel(a->right,k) ;
}
BTNode* BinaryTreeFind(BTNode* root, BinaryType a)
{
	if (root->a == a)
	{
		return root;
	}
	if (root == NULL)
	{
		return NULL;
	}
	BTNode* c= BinaryTreeFind(root->left, a);
	if (c)
	{
		return c;
	}
	BTNode* d=BinaryTreeFind(root->right, a);
	if (d)
	{
		return d;
	}
	else
	{
		return NULL;
	}

}
void Level_Order(BTNode* a,int k)
{
	if (k == 1 && a != NULL)
	{
		printf("%d ", a->a);
		return;
	}
	if (a == NULL)
	{
		return;
	}
	k--;
	Level_Order(a->left, k);
	Level_Order(a->right, k);
}
void Level_Order_Queue(BTNode* a)
{
	Queue b;
	QueueInit(&b);
	QueuePush(&b, a);
	while (b.head!=NULL&&b.tail!=NULL)
	{
		BTNode* front = QueueFront(&b);
		QueuePop(&b);
		printf("%d ", front->a);
		if (front->left != NULL)
		{
			QueuePush(&b, front->left);
		}
		if (front->right != NULL)
		{
			QueuePush(&b, front->right);
		}
	}

}
bool TreeComplete(BTNode* a)
{
	Queue b;
	QueueInit(&b);
	QueuePush(&b, a);
	while (b.head != NULL && b.tail != NULL)
	{
		BTNode* front = QueueFront(&b);
		QueuePop(&b);
		if (front == NULL)
		{
			break;
		}
		else
		{
			QueuePush(&b, front->left);
			QueuePush(&b, front->right);
		}
	}
	while (b.head != NULL && b.tail != NULL)
	{
		BTNode* front = QueueFront(&b);
		QueuePop(&b);
		if (front != NULL)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}
void Tree_Destroy(BTNode* a)
{
	if (a == NULL)
	{
		return;
	}
	Tree_Destroy(a->left);
	Tree_Destroy(a->right);
	free(a);
}
int sumOfLeftLeaves(BTNode* root) {
	BTNode* tem = root->left;
	while (1)
	{
		if (tem == NULL ||tem->left == NULL)
		{
			break;
		}
		tem = tem->left;
	}
	int a = tem->a;
	tem = root->right;
	int b = 0;
	if (tem == NULL || tem->left == NULL)
	{
		return a + b;
	}
	while (1)
	{
		if (tem == NULL || tem->left == NULL)
		{
			break;
		}
		tem = tem->left;
	}
	 b = tem->a;
	return a + b;
}
int main()
{
	BTNode* a = CreatBinaryTree();
	int b= sumOfLeftLeaves(a);
	printf("%d", b);
}
